package www.openteam.com.model.policy;

import android.support.annotation.NonNull;

import java.util.List;

import www.openteam.com.OTApplication;
import www.openteam.com.base.BaseModel;
import www.openteam.com.bean.ApprovalStatu;
import www.openteam.com.bean.PayableApproval;
import www.openteam.com.bean.ReceivableApproval;
import www.openteam.com.exception.ApiException;
import www.openteam.com.subscribe.CommonSubscriber;
import www.openteam.com.transformer.CommonTransformer;

/**
 * Created by kylin on 2016/11/4.
 */

public class ReceivableApprovalModel extends BaseModel {

    private boolean isQuery = false;

    private boolean isApproval = false;

    private boolean isUnApproval = true;

    //    queryPayableApproval(@Query("vendno")
    // String vendno, @Query("startDate") long startDate,
    // @Query("endDate") long endDate, @Query("pageSize") int PageSize);

    public boolean queryReceivableApproval(@NonNull String custid,
                                           long startDate,
                                           long endDate,int orderStatus,
                                           String stype, String moreCom,
                                           @NonNull int PageSize, @NonNull final
                                           ReceivableApprovalOnListener receivableApprovalOnListener) {

        httpApiService.queryReceivableApproval(custid, startDate, endDate,orderStatus, stype, moreCom, PageSize)
                .compose(new CommonTransformer<ReceivableApproval>())
                .subscribe(new CommonSubscriber<ReceivableApproval>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(ReceivableApproval receivableApproval) {
                        isQuery = true;
                        receivableApprovalOnListener.queryRASuccess(receivableApproval.getData());
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isQuery = false;
                        receivableApprovalOnListener.queryRAFail(e.message);
                    }
                });
        return isQuery;
    }


    public boolean reApprovalUpdate(@NonNull int id, @NonNull
    final ReceivableOrderAOnListener receivableOrderAOnListener) {
        httpApiService.reApprovalUpdate(id)
                .compose(new CommonTransformer<ApprovalStatu>())
                .subscribe(new CommonSubscriber<ApprovalStatu>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(ApprovalStatu approvalStatu) {
                        isApproval = true;
                        receivableOrderAOnListener.approvalRASuccess(approvalStatu);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isApproval = false;
                        receivableOrderAOnListener.approvalRAFail(e.message);
                    }
                });
        return isApproval;
    }


    public boolean unReApprovalUpdate(@NonNull int id, @NonNull final
    ReceivableOrderUOnListener receivableOrderUOnListener) {
        httpApiService.unPaApprovalUpdate(id)
                .compose(new CommonTransformer<ApprovalStatu>())
                .subscribe(new CommonSubscriber<ApprovalStatu>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(ApprovalStatu approvalStatu) {
                        isUnApproval = true;
                        receivableOrderUOnListener.unApprovalRASuccess(approvalStatu);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isUnApproval = false;
                        receivableOrderUOnListener.unApprovalRAFail(e.message);
                    }
                });
        return isUnApproval;
    }

    public interface ReceivableApprovalOnListener {
        void queryRASuccess(List<ReceivableApproval.DataEntity> orderApprovals);

        void queryRAFail(String failMsg);
    }

    public interface ReceivableOrderAOnListener {
        void approvalRASuccess(ApprovalStatu aprovalStatu);

        void approvalRAFail(String failMsg);

    }

    public interface ReceivableOrderUOnListener {
        void unApprovalRASuccess(ApprovalStatu aprovalStatu);

        void unApprovalRAFail(String failMsg);
    }

}
